import Taro from "@tarojs/taro";
import { toast } from "@/helper/utils";

export const REQUEST_OK = 1; // 后端请求成功错误码
export const I2_REQUEST_OK = 0; // i2请求成功错误码
export const QX_REQUEST_OK = 200; // 全象请求成功错误码
export const SUCCESS_CODE = 200; // 接口请求成功

export default {
  /**
   * 统一转化请求返回格式拦截器
   * @param chain
   */
  parseData(chain) {
    return chain.proceed(chain.requestParams).then((resp) => {
      Taro.hideLoading();
      const { statusCode, data } = resp;
      if (statusCode === SUCCESS_CODE) {
        const { status, msg } = data;
        if (status === QX_REQUEST_OK) {
          data.isSuccess = true;
          return data;
        }
        if (msg) {
          toast({ title: msg });
        }
      } else {
        toast({
          title: "服务端出错",
        });
      }
      return resp;
    });
  },
};
